package com.example.smartlearning.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.smartlearning.dao.NewDao;
import com.example.smartlearning.dao.TieZiDao;
import com.example.smartlearning.entity.NewEntity;
import com.example.smartlearning.entity.ResultEntity;
import com.example.smartlearning.entity.TieZiEntity;
import com.example.smartlearning.service.NewService;
import com.example.smartlearning.service.TieZiService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class TieZiServiceImpl extends ServiceImpl<TieZiDao, TieZiEntity> implements TieZiService {
    private static final Logger logger = LoggerFactory.getLogger(TieZiServiceImpl.class);//日志。getLogger需要传入当前的类名

    @Resource
    TieZiDao tieZiDao;

    /**
     * 获取所有帖子
     *
     * @return 返回
     */
    @Override
    public String getAll() {
        try {
            return ResultEntity.success(tieZiDao.getAll());
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    /**
     * 根据关键词获取帖子
     *
     * @param keyWord 关键词
     * @return 返回
     */
    @Override
    public String getByKeyWord(String keyWord) {
        try {
            return ResultEntity.success(tieZiDao.getByKeyWord(keyWord));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    @Override
    public String addNew(String title, String content, int userId, String keyWord) {
        try {
            return ResultEntity.success(tieZiDao.addNew(title, content, userId, keyWord));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    @Override
    public String deleteById(int id) {
        try {
            return ResultEntity.success(tieZiDao.deleteById(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    @Override
    public String updateTzUpvote(int id) {
        try {
            return ResultEntity.success(tieZiDao.updateTzUpvote(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    @Override
    public String cancelTzUpvote(int id) {
        try {
            return ResultEntity.success(tieZiDao.cancelTzUpvote(id));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

}
